package com.ksl.kinglockguard.widget;

import java.lang.ref.WeakReference;
import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.Date;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.graphics.Typeface;
import android.os.Handler;
import android.provider.Settings;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.TextView;

import com.ksl.kinglockguard.R;
import com.lsk.open.core.MyLog;

public class StatusViewManager implements DateTimeControl
{
	private static final String TAG = "StatusViewManager";
    private static final String SYSTEM = "/system/fonts/";
    private static final String SYSTEM_FONT_TIME_BACKGROUND = SYSTEM + "Clockopia.ttf";
    private static final String SYSTEM_FONT_TIME_FOREGROUND = SYSTEM + "AndroidClock_Highlight.ttf";
    
    private final static String M12 = "h:mm";
    private final static String M24 = "kk:mm";
    
	private TextView mDateView;
	private TextView mTimeView,mWeedendTextView;
	
	private String mDateFormat;
	private String mFormat;
	
	//private AmPm mAmPm;
    private Calendar mCalendar;
    public ContentObserver mFormatChangeObserver;
    public BroadcastReceiver mIntentReceiver;
   
    private final Handler mHandler = new Handler();
    
    private static  Typeface sBackgroundFont=null;
    private static  Typeface sForegroundFont=null;
    
    private static Context mContext;
    private View mParentView;
    static 
    {
        try {
			sBackgroundFont = Typeface.createFromFile(SYSTEM_FONT_TIME_BACKGROUND);
			sForegroundFont = Typeface.createFromFile(SYSTEM_FONT_TIME_FOREGROUND);
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
	public StatusViewManager(View view, Context context)
	{
		mContext = context;
		mParentView = view;
		initViews();
		refreshDate();
	}
	
	private View findViewById(int id) 
	{
        return mParentView.findViewById(id);
    }
	
    private void refreshDate()
    {
    	if (mDateView != null)
    	{
    		String dateorWeekend=(String) DateFormat.format(mDateFormat, new Date());
    		
    		String[] strs=dateorWeekend.split(" ");
    		
    		
    		//mDateView.setText(DateFormat.format(mDateFormat, new Date()));
    		if(strs!=null&&strs.length>1)
    		mDateView.setText(strs[0]);
    		if(mWeedendTextView!=null){
    			mWeedendTextView.setText(strs[1]);
    		}
    	
    	}
    }
	
    private String getString(int id)
    {
    	return mContext.getResources().getString(id);
    }
    
    class AmPm {
        private TextView mAmPmTextView;
        private String mAmString, mPmString;

        AmPm(Typeface tf) {
            mAmPmTextView = (TextView)findViewById(R.id.tv_week);
            if (mAmPmTextView != null && tf != null) {
                mAmPmTextView.setTypeface(tf);
            }

            String[] ampm = new DateFormatSymbols().getAmPmStrings();
            mAmString = ampm[0];
            mPmString = ampm[1];
        }

        void setShowAmPm(boolean show) {
            if (mAmPmTextView != null) {
                mAmPmTextView.setVisibility(show ? View.VISIBLE : View.GONE);
            }
        }

        void setIsMorning(boolean isMorning) {
            if (mAmPmTextView != null) {
                mAmPmTextView.setText(isMorning ? mAmString : mPmString);
            }
        }
    }
    
    private static class TimeChangedReceiver extends BroadcastReceiver {
        private WeakReference<StatusViewManager> mStatusViewManager;
        //private Context mContext;

        public TimeChangedReceiver(StatusViewManager status) {
        	mStatusViewManager = new WeakReference<StatusViewManager>(status);
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            // Post a runnable to avoid blocking the broadcast.
            final boolean timezoneChanged =
                    intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED);
            final StatusViewManager status = mStatusViewManager.get();
            if (status != null) {
            	status.mHandler.post(new Runnable() {
                    public void run() {
                        if (timezoneChanged) {
                        	status.mCalendar = Calendar.getInstance();
                        }
                        status.updateTime();
                        status.refreshDate();
                    }
                });
            } else {
                try {
                	mContext.unregisterReceiver(this);
                } catch (RuntimeException e) {
                    // Shouldn't happen
                }
            }
        }
    };
    private static class FormatChangeObserver extends ContentObserver {
        private WeakReference<StatusViewManager> mStatusViewManager;
        //private Context mContext;
        public FormatChangeObserver(StatusViewManager status) {
            super(new Handler());
            mStatusViewManager = new WeakReference<StatusViewManager>(status);
        }
        @Override
        public void onChange(boolean selfChange) {
        	StatusViewManager mStatusManager = mStatusViewManager.get();
            if (mStatusManager != null) {
            	mStatusManager.setDateFormat();
            	mStatusManager.updateTime();
            } else {
                try {
                	mContext.getContentResolver().unregisterContentObserver(this);
                } catch (RuntimeException e) {
                    // Shouldn't happen
                }
            }
        }
    }
    
    private void updateTime() 
    {
        mCalendar.setTimeInMillis(System.currentTimeMillis());

        CharSequence newTime = DateFormat.format(mFormat, mCalendar);
        mTimeView.setText(newTime);
       // mAmPm.setIsMorning(mCalendar.get(Calendar.AM_PM) == 0);
    }
    
    private void setDateFormat() 
    {
       /* mFormat = android.text.format.DateFormat.is24HourFormat(mContext)
            ? M24 : M12;*/
        mFormat = M24 ;
        //mAmPm.setShowAmPm(mFormat.equals(M24));
    }

	@Override
	public void registerComponent() 
	{
		// TODO Auto-generated method stub
		MyLog.i(TAG, "registerComponent()");
		
        if (mIntentReceiver == null) {
            mIntentReceiver = new TimeChangedReceiver(this);
            IntentFilter filter = new IntentFilter();
            filter.addAction(Intent.ACTION_TIME_TICK);
            filter.addAction(Intent.ACTION_TIME_CHANGED);
            filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
            mContext.registerReceiver(mIntentReceiver, filter);
        }

        if (mFormatChangeObserver == null) {
            mFormatChangeObserver = new FormatChangeObserver(this);
            mContext.getContentResolver().registerContentObserver(
                    Settings.System.CONTENT_URI, true, mFormatChangeObserver);
        }
        
        updateTime();
	}

	@Override
	public void unregisterComponent()
	{
		// TODO Auto-generated method stub
		MyLog.i(TAG, "unregisterComponent()");
        if (mIntentReceiver != null) {
        	mContext.unregisterReceiver(mIntentReceiver);
        }
        if (mFormatChangeObserver != null) {
        	mContext.getContentResolver().unregisterContentObserver(
                    mFormatChangeObserver);
        }
        mFormatChangeObserver = null;
        mIntentReceiver = null;
	}

	@Override
	public void initViews() 
	{
		// TODO Auto-generated method stub
		mDateView = (TextView)findViewById(R.id.tv_date);
		mWeedendTextView = (TextView)findViewById(R.id.tv_week);
    	mDateFormat = getString(R.string.lock_month_day_year);
    	
    	mTimeView = (TextView) findViewById(R.id.tv_time);
    	Typeface fontFace = Typeface.createFromAsset(mContext.getAssets(),
                "fonts/AndroidClock.ttf");
        if(fontFace!=null)
    	mTimeView.setTypeface(fontFace); 
//    	if(sForegroundFont!=null)
//    		mTimeView.setTypeface(sForegroundFont);
//    	else{
//    		String familyName = "Times New Roman";
//	    	Typeface font = Typeface.create(familyName,Typeface.NORMAL);
//	    	mTimeView.setTypeface(font);
//    	}
        mCalendar = Calendar.getInstance();
        
        setDateFormat();
        registerComponent();
	}
    
}
